/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Client;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Anastasia
 */
public class Add extends JFrame implements ActionListener
{
    public class SendEmployee implements ActionListener {


        public void actionPerformed(ActionEvent arg0)
        {

            try {
                Employee employee = new ClientEmployee(name.getText(),surname.getText(), lastname.getText(), pay.getText(), phone.getText());
                InetAddress addr = InetAddress.getByName(null);



                Socket socket = new Socket(addr, 8080);

                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream()));
                out.writeObject(new Exchanger(1,employee));

                out.flush();
                out.close();


              //
              //  String result = in.readLine();
             //   res.setText(result);
              //  panel.repaint();

                socket.close();
            }
            catch(UnknownHostException e)
            {

            }

            catch(IOException e)
            {

            }

          // dispose();  закрыть окно по кнопке

        }
    }

    private JButton button;
    private  JPanel panel = new JPanel();
    private JLabel Label;
    private JLabel namelabel= new JLabel("Имя");;
    private JLabel surnamelabel= new JLabel("Отчество");;
    private JLabel lastnamelabel = new JLabel("Фамилия");;
    private JLabel departmentlabel = new JLabel("Департамент");
    private JLabel paylabel = new JLabel("Зарплата");
    private JLabel  phonelabel = new JLabel("Телефон");
    private JTextField name = new JTextField(10);
    private JTextField surname;
    private JTextField lastname;
    private JTextField department;
    private JTextField pay;
    private JTextField phone;
    private JButton newDeparmentbutton;
    private JButton existDepartmentbutton;
    private JTextField res;


    public Add()
    {

    }

    public void actionPerformed(ActionEvent arg0)
    {
        try{
        InetAddress addr = InetAddress.getByName(null);

        Socket socket = new Socket(addr, 8080);

        //         BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintWriter outer = new PrintWriter(new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream())), true);
        outer.println(1);  //это будет команда


        socket.close();  }

        catch(UnknownHostException e)
        {

        }

        catch(IOException e)
        {

        }





        this.setVisible(true);
        setBounds(600,600,700,700);
        panel = new JPanel();
        panel.setLayout(null);

        res = new JTextField();
        res.setBounds(100, 30, 400, 30);

        panel.add(res);
        res.setVisible(false);


        namelabel = new JLabel("Имя");
        namelabel.setBounds(100,100,80,30);
        panel.add(namelabel) ;

        name = new JTextField();
        name.setBounds(200, 100, 200, 30);

        panel.add(name);


        surnamelabel = new JLabel("Отчество");
        surnamelabel.setBounds(100,150,80,30);
        panel.add(surnamelabel) ;

        surname = new JTextField();
        surname.setBounds(200, 150, 200, 30);

        panel.add(surname);


        lastnamelabel = new JLabel("Фамилия");
        lastnamelabel.setBounds(100,200,80,30);
        panel.add(lastnamelabel) ;

        lastname = new JTextField();
        lastname.setBounds(200, 200, 200, 30);

        panel.add(lastname);

        departmentlabel = new JLabel("Департамент");
        departmentlabel.setBounds(100,250,80,30);
        panel.add(departmentlabel) ;

        //department = new JTextField();
        //department.setBounds(200, 250, 200, 30);


    //    panel.add(department);


        newDeparmentbutton = new JButton("Новый отдел");
        newDeparmentbutton.setBounds(200,250,200,30);
        panel.add( newDeparmentbutton);


        existDepartmentbutton = new JButton("Существуюший отдел");
        existDepartmentbutton.setBounds(410,250,200,30);
        panel.add(existDepartmentbutton);


        paylabel = new JLabel("Зарплата");
        paylabel.setBounds(100,300,80,30);
        panel.add(paylabel) ;

        pay = new JTextField();
        pay.setBounds(200, 300, 200, 30);

        panel.add(pay);



        phonelabel = new JLabel("Телефон");
        phonelabel.setBounds(100,350,80,30);
        panel.add(phonelabel) ;

        panel.validate();


        phone = new JTextField();
        phone.setBounds(200, 350, 200, 30);

        panel.add(phone);

        button = new JButton("ОК");
        button.setBounds(150,400,200,30);
        panel.add(button);

        button.addActionListener(new SendEmployee());
        panel.validate();



        this.add(panel);
        setPreferredSize(new Dimension(500, 800));



    }
}
